$(function(){
	$(".add-contest-but").click(function(){
		//$(".team-div").show();
		var school_select = "";
		$.ajax({
	        url:'/preliminaryContest/querySchoolAll.json',
	        type:'POST',
	        async:false,
	        dataType:"json",
	        success:function(data) {
	        	$(data).each(function(i,obj){
	        		school_select += "<option value='"+obj.id+"'>"+obj.name+"</option>";
	        	});
	        }
	    });
		
		var str = "<tr>" +
						"<td name='date' style='width: 80px; text-align: center;'>" +
							"<span name='date-span'></span>" +
							//"<input  type='text' name='date-input' style='width: 80px;'/>" +
							"<input readonly='readonly' name='date-input' class='Wdate span9' type='text' onFocus='new WdatePicker()'/>"+
						"</td>" +
						"<td name='time' style='width: 80px; text-align: center;'>" +
							"<span name='time-span'></span>" +
							"<input  type='text' class='input-append bootstrap-timepicker' name='time-input' style='width: 80px;'/>" +
						"</td>" +
						"<td name='teamName' style='width: 80px; text-align: center;'>" +
							"<span name='teamName-span'></span>" +
							"<input  type='hidden' name='teamName-input' style='width: 80px;'/>" +
							"<a class='show-btn btn btn-info btn-small' name='teamName-a'>对阵球队1</a>"+
						"</td>" +
						"<td name='teamTwoName' style='width: 80px; text-align: center;'>" +
							"<span name='teamTwoName-span'></span>" +
							"<input  type='hidden' name='teamTwoName-input' style='width: 80px;'/>" +
							"<a class='show-btn btn btn-info btn-small' name='teamTwoName-a'>对阵球队2</a>"+
						"</td>" +
						"<td name='schoolName' style='width: 80px; text-align: center;'>" +
							"<span name='schoolName-span'></span>" +
							//"<input  type='text' name='schoolName-input' style='width: 80px;'/>" +
							"<select name='school-select' style='width: 130px;'>" +
							 	school_select +
							"</select>"+
						"</td>" +
						"<td name='site' style='width: 80px; text-align: center;'>" +
							"<span name='site-span'></span>" +
							"<input  type='text' name='site-input' style='width: 80px;'/>" +
						"</td>" +
						"<td style='width: 80px; text-align: center;'>" +
							"<input type='button' class='btn btn-info btn-inverse update-btn' onclick='updateTeam(this)' style='display: none;' value='修改'/>" +
							"<input type='button' class='btn btn-info btn-inverse insert-btn' onclick='insertTeam(this)' value='保存'/>" +
							"<input type='button' class='btn btn-info btn-inverse cancel-btn' onclick='cancelTeam(this)' value='取消'/>" +
						"</td>" +
				  "</tr>";
		$(".contest-list tbody").append(str);
		addTeam();
	});
	
	$(".team-list").find("tr").click(function(){
		var teamTd = $(this).find("[name='teamName']");
		if(teamTd.length > 0){
			var teamName = teamTd.text();
			var teamId = teamTd.attr("id");
			var contestTeam = $("[team='contest-team-div']");
			var parent = contestTeam.parent();
			if(contestTeam.attr("name") == "teamName-a"){
				var teamId2 = parent.parent().find("[name='teamTwoName-input']").val();
				if(teamId != teamId2){
					parent.find("[name='teamName-span']").text(teamName).show();
					parent.find("[name='teamName-input']").val(teamId);
					contestTeam.hide();
				}else{
					alert("请选择其他球队");
					return;
				}
			}else{
				var teamId2 = parent.parent().find("[name='teamName-input']").val();
				if(teamId != teamId2){
					parent.find("[name='teamTwoName-span']").text(teamName).show();
					parent.find("[name='teamTwoName-input']").val(teamId);
					contestTeam.hide();
				}else{
					alert("请选择其他球队");
					return;
				}
			}
			$("#team-div").hide();
		}
	});
	
	$(".team-model-list").find("tr").click(function(){
		var teamTd = $(this).find("[name='team']");
		if(teamTd.length > 0){
			var teamName = teamTd.text();
			var teamId = teamTd.attr("id");
			var contestTeam = $("[team='contest-team-div']");
			var parent = contestTeam.parent();
			if(contestTeam.attr("name") == "teamName-a"){
				var teamId2 = parent.parent().find("[name='teamTwoName-input']").val();
				if(teamId != teamId2){
					parent.find("[name='teamName-span']").text(teamName).show();
					parent.find("[name='teamName-input']").val(teamId);
					contestTeam.hide();
				}else{
					alert("请选择其他球队");
					return;
				}
			}else{
				var teamId2 = parent.parent().find("[name='teamName-input']").val();
				if(teamId != teamId2){
					parent.find("[name='teamTwoName-span']").text(teamName).show();
					parent.find("[name='teamTwoName-input']").val(teamId);
					contestTeam.hide();
				}else{
					alert("请选择其他球队");
					return;
				}
			}
//			$('#upload-model').modal('hide');
			$(".close").click();
		}
	});
	
	$(".btn-competition-team").click(function(){
		var seasonId = $("#seasonId").val();
        window.location.href = "/competitionTeam/list.htm?seasonId="+seasonId;
	});
	
	$(".btn-preliminary-contest").click(function(){
		var seasonId = $("#seasonId").val();
        window.location.href = "/preliminaryContest/list.htm?seasonId="+seasonId+"&type=1";
	});
	
	$(".btn-eliminate-contest").click(function(){
		var seasonId = $("#seasonId").val();
        window.location.href = "/preliminaryContest/list.htm?seasonId="+seasonId+"&type=2";
	});
	
	$(".export-btn").click(function(){
		window.location.href = "/preliminaryContest/createContestTable.json";
	});
	
	$(".import-btn").click(function(){
		$('#import-model').modal('show');
	});
	
	$(".upload-btn").click(function(){
		$('#upload-model').modal('show');
	});
	
	$("#uploadImgForm").validate( {
        submitHandler : function(form) {
    		$("#uploadImgForm").ajaxSubmit(function(date){
                if(date.suc){
                    alert("操作成功");
                    var seasonId = $("#seasonId").val();
                    window.location.href = "/preliminaryContest/list.htm?seasonId="+seasonId+"&type=2";
                }else{
                	alert("操作失败");
                }
            });
        },
        highlight: function (e) {
            $(e).closest('.control-group').removeClass('info').addClass('error');
        }
    });
	
	$(".delete-btn").click(function(){
		var eliminateImg = $("#eliminateImg").attr("src");
		if(eliminateImg == null || eliminateImg == ''){
			alert("没有可删除的图片");
			return;
		}
		var seasonId = $("#seasonId").val();
		$.ajax({
	        url:'/preliminaryContest/deleteEliminateImg.json',
	        type:'POST',
	        async:false,
	        data:{seasonId:seasonId},
	        dataType:"json",
	        success:function(data) {
	        	if(data){
	        		alert("操作成功");
	        		window.location.href = "/preliminaryContest/list.htm?seasonId="+seasonId+"&type=2";
	        	}else{
	        		alert("操作失败");
	        	}
	        }
	    });
	});
	
	$('#id-input-file').ace_file_input({
        no_file: '没有文件 ...',
        btn_choose: 'Choose',
        btn_change: 'Change',
        droppable: false,
        onchange: null,
        thumbnail: false
    });
	
	$('#id-input-file2').ace_file_input({
        no_file: '没有文件 ...',
        btn_choose: 'Choose',
        btn_change: 'Change',
        droppable: false,
        onchange: null,
        thumbnail: false
    });
	
});

insertTeam = function(obj){
	var parent = $(obj).parent().parent();
	
	var seasonId = $("#seasonId").val();
	var type = $("#type").val();
	var id = parent.find("[name='id']").val();
	var date = parent.find("[name='date-input']").val();
	var time = parent.find("[name='time-input']").val();
	var teamName = parent.find("[name='teamName-span']").text();
	var teamTwoName = parent.find("[name='teamTwoName-span']").text();
	var teamId = parent.find("[name='teamName-input']").val();
	var teamIdTwo = parent.find("[name='teamTwoName-input']").val();
	var schoolName = parent.find("[name='school-select'] option:selected").text();
	var schoolId = parent.find("[name='school-select'] option:selected").val();
	var site = parent.find("[name='site-input']").val();
	
	if(date == null || date == ''){
		alert("请填写日期");
		return;
	}
	if(time == null || time == ''){
		alert("请填写时间");
		return;
	}
	if(teamId == null || teamId == ''){
		alert("请选择对阵球队1");
		return;
	}
	if(teamIdTwo == null || teamIdTwo == ''){
		alert("请选择对阵球队2");
		return;
	}
	if(schoolId == null || schoolId == ''){
		alert("请选择校区");
		return;
	}
	if(site == null || site == ''){
		alert("请填写场地");
		return;
	}
	
	$.ajax({
        url:'/preliminaryContest/insertOrUpdate.json',
        data:{id:id,seasonId:seasonId,type:type, date:date, time:time, teamId:teamId,
        	teamIdTwo:teamIdTwo, schoolId:schoolId, site:site},
        type:'POST',
        dataType:"json",
        success:function(data) {
        	if(data > 0){
        		alert("操作成功");
            	parent.find("[name='date-span']").text(date).show();
            	parent.find("[name='time-span']").text(time).show();
            	parent.find("[name='teamName-span']").text(teamName).show();
            	parent.find("[name='teamTwoName-span']").text(teamTwoName).show();
            	parent.find("[name='schoolName-span']").text(schoolName).show();
            	parent.find("[name='site-span']").text(site).show();
            	
            	parent.find("[name='date-input']").attr("type","hidden");
            	parent.find("[name='time-input']").attr("type","hidden");
            	parent.find("[name='teamName-a']").hide();
            	parent.find("[name='teamTwoName-a']").hide();
            	parent.find("[name='school-select']").hide();
            	parent.find("[name='site-input']").attr("type","hidden");
            	if(id == null || id == ''){
        			var str = "<input type='hidden' name='id' value="+data+" />";
        			$(obj).parent().append(str);
        		}
            	$(obj).hide();
            	parent.find(".cancel-btn").hide();
            	parent.find(".update-btn").show();
        	}else{
        		alert("系统错误");
        	}
        }
    });
};

cancelTeam = function(obj){
	var parent = $(obj).parent().parent();
	var id = parent.find("[name='id']").val();
	if(id != null && id != ''){
		$.ajax({
	        url:'/preliminaryContest/queryContestById.json',
	        type:'POST',
	        async:false,
	        data:{id:id},
	        dataType:"json",
	        success:function(data) {
	        	parent.find("[name='date-span']").text(data.date).show();
            	parent.find("[name='time-span']").text(data.time).show();
            	parent.find("[name='teamName-span']").text(data.teamName).show();
            	parent.find("[name='teamTwoName-span']").text(data.teamTwoName).show();
            	parent.find("[name='schoolName-span']").text(data.schoolName).show();
            	parent.find("[name='site-span']").text(data.site).show();
            	parent.find("[name='teamName-input']").val(data.teamId);
            	parent.find("[name='teamTwoName-input']").val(data.teamIdTwo);
            	parent.find("[name='school-select']").val(data.schoolId);
            	
            	parent.find("[name='date-input']").attr("type","hidden");
        		parent.find("[name='time-input']").attr("type","hidden");
        		parent.find("[name='teamName-a']").hide();
        		parent.find("[name='teamTwoName-a']").hide();
        		parent.find("[name='school-select']").hide();
        		parent.find("[name='site-input']").attr("type","hidden");
        		$(obj).hide();
        		parent.find(".insert-btn").hide();
        		parent.find(".update-btn").show();
	        }
	    });
		
	}else{
		parent.remove();
	}
};

updateTeam = function(obj){
	var parent = $(obj).parent().parent();
	parent.find("[name='date-span']").hide();
	parent.find("[name='time-span']").hide();
	parent.find("[name='teamName-span']").hide();
	parent.find("[name='teamTwoName-span']").hide();
	parent.find("[name='schoolName-span']").hide();
	parent.find("[name='site-span']").hide();
	
	parent.find("[name='date-input']").attr("type","text");
	parent.find("[name='time-input']").attr("type","text");
	parent.find("[name='teamName-a']").show();
	parent.find("[name='teamTwoName-a']").show();
	parent.find("[name='school-select']").show();
	parent.find("[name='site-input']").attr("type","text");
	$(obj).hide();
	parent.find(".insert-btn").show();
	parent.find(".cancel-btn").show();
	addTeam();
};

addTeam = function(){
	$("[name='teamName-a']").unbind().bind("click",function(){
		var type = $("#type").val();
		$("[team='contest-team-div']").attr("team","");
		$(this).attr("team","contest-team-div");
		if(type == 1){
			if($("#team-div").attr("style") == 'display: none;'){
				$("#team-div").show();
			}
		}else{
			$("#team-model").modal('show');
		}
	});
	
	$("[name='teamTwoName-a']").unbind().bind("click",function(){
		var type = $("#type").val();
		$("[team='contest-team-div']").attr("team","");
		$(this).attr("team","contest-team-div");
		if(type == 1){
			if($("#team-div").attr("style") == 'display: none;'){
				$("#team-div").show();
			}
		}else{
			$("#team-model").modal('show');
		}
	});
};
